package stat;

import inventory.EquipLocation;
import loadsave.LoadSaveable;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import model.ModelObject;

public class StatManager extends ModelObject implements LoadSaveable {

    private StatList sl;

    public StatManager( StatList sl ) {
    	super("stat_manager");
        this.sl = sl;
    }
    
    /**
     * attempt to spend a stat point to upgrade
     * the given stattype
     * @param st
     */
    public void addPointToStat( StatType st ) {
        if(sl.decrement(StatType.STATPOINT)) {
            sl.increment(st);
        }
        this.notifyObservers();
    }
    
    /**
     * get the statlist that this manager points to
     * @return
     */
    public StatList getStatList() {
        return sl;
    }

    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                LoadSaveObject lso = StatManager.super.getMemento().getState();
                Iterator<LoadSaveObject> i = lso.getIterator();
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "StatManager");
                
                for (i.init(); i.hasNext(); i.next()) {
                    toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
                }
                
                LoadSaveObject stats = sl.getMemento().getState();
                stats.setName("sl");
                toReturn.add(stats.getName(), stats);
                
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
                StatManager.super.getMemento().restoreState(lso);
                sl = new EntityStatList();
                sl.getMemento().restoreState(lso.get("sl"));
            }
            
        };
    }
    
}
